package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.CheckGroup;
import com.xrit.sunnyday.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 查询所有（分页）
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<CheckGroup> pageInfoList = checkGroupService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfoList.getTotal(), pageInfoList.getList());
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] ids) {
        checkGroupService.add(checkGroup, ids);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除
     */
    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        checkGroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 编辑，根据id查询所编辑的检查组
     */
    @GetMapping("/updateById")
    public Result updateById(Integer id) {
        CheckGroup checkGroup = checkGroupService.updateById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkGroup);
    }

    /**
     * 查询该检查组对应的检查项
     * @param id
     * @return
     */
    @GetMapping("/updateCheckitem")
    public Result updateCheckitem(Integer id) {
        List<Integer> checkitemIds = checkGroupService.updateCheckitem(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
    }

    /**
     * 编辑
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        checkGroupService.update(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询检查组（不带分页）
     */
    @GetMapping("/findAll")
    public Result findAll(){
       List<CheckGroup> list= checkGroupService.findAll();
       return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }
}
